package com.ruoyi.web.controller.hospital;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.hospital.domain.HPatient;
import com.ruoyi.hospital.domain.ViewCpjsPatlist;
import com.ruoyi.hospital.service.IHpatientService;
import com.ruoyi.hospital.service.IViewCpjsPatlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 患者controller
 * @author liz
 * @date 2023-10-21 8:59
 */
@RestController
@RequestMapping("hospital/cpjs")
public class ViewCpjsPatlistController extends BaseController {

    @Autowired
    IViewCpjsPatlistService viewCpjsPatlistService;

    @Autowired
    IHpatientService hpatientService;

    @GetMapping("list")
    public AjaxResult list() {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<ViewCpjsPatlist> list = viewCpjsPatlistService.list(new QueryWrapper<ViewCpjsPatlist>().ge("effective_e", LocalDate.now()));
        DynamicDataSourceContextHolder.clearDataSourceType();
        list.forEach(item -> {
            HPatient hPatient = hpatientService.getOne(new QueryWrapper<HPatient>().eq("inpatient_no", item.getInpatientNo()));
            if (ObjectUtil.isEmpty(hPatient)) {
                hPatient = new HPatient();
                BeanUtil.copyProperties(item, hPatient);
                hpatientService.saveOrUpdate(hPatient);
            }
        });
        return success();
    }
}
